// 引入接口文件
import { getNewsList, parentCode , getProductId,deleteDetail } from './infoListServ'

// 对象和JSON字符串互转
import { parse } from 'qs'
// 弹出消息框
import { message } from 'antd'
// 日期处理对象
import moment from 'moment'
/* isEmpty 对象是否为空
 * isArray 对象是否数组
 * cloneDeep 对象深拷贝
 */
import { isEmpty, isArray, cloneDeep, merge } from 'lodash'
// 引入路由对象
import { hashHistory } from 'dva/router'
import { makeAddress } from "utils/util"

// 初始默认状态
const state = {
  channel:1,
  biz: {
		queryForm: {}
	},
  areaList: [],
  contentId:''
}

//处理select所需数据格式
function HandleSelectOption(arr){
  let newArr =[]
  arr.map( item=>{
    let OpiconObj ={}
    OpiconObj.value = item
    OpiconObj.label = item
    newArr.push(OpiconObj)
  })
  return newArr
}

// 声明module
const tmpModule = {
  namespace: 'infoListAdminModel',
  // 默认状态
  state: cloneDeep(state),
  // 入口函数(先执行)
  subscriptions: {
    setup({ dispatch, history }) {
      history.listen(({ pathname, query }) => {
        // 页面路由匹配时执行
        if ('' + tmpModule.pathname === '' + pathname) {
          //每次进来页面更新数据 重置state
          dispatch({ type: 'updateStore', payload: cloneDeep(state) });
          dispatch({ type: 'getAreaList', payload: {} })
          dispatch({ type: 'getBusinessId', payload: {} })
        } // end if
      }) // end listen
    },
  },

  // 方法列表
  effects: {
    //获取项目id
    *getBusinessId({ payload }, { put, call, select }) {
      try {
        const { rows } = yield call(getNewsList, {channel:1 })
        //过滤出businessId
        let businessIdList = []
        rows.map(item =>{
          businessIdList.push(item.businessId)
        })
        businessIdList = [...new Set(businessIdList)]
        businessIdList = HandleSelectOption(businessIdList)
        yield put({ type: 'updateStore', payload: { businessIdList} })
      } catch (e) {

      }
    },
    //删除资讯
    *deleteContent({ payload }, { put, call, select }) {
      try {
        let { contentId } = payload
        // contentId = Number(contentId)
        console.log(contentId)
        let state = yield select(state => state[tmpModule.namespace])
        const data = yield call(deleteDetail, { contentId: contentId })
        if (data.success) {
          message.success('删除成功')
          state.biz.refreshListDatas(true)
        }
      } catch (e) {

      }
    },
    //获取级联列表
    *getAreaList({ payload }, { put, call, select }) {
      try {
        const { data, success } = yield call(parentCode);
        if(success) {
          let areaList = []
          let arr = [data[0]]
          let attr = [data[data.length-1]]
          data[0] = {
            label:"北京",
            value:"110100",
            children:arr
          }
          data[data.length-1]={
            label:"澳门",
            value:"820000",
            children:attr
          }
          //去掉第三级，
          areaList = makeAddress(data,2)
          yield put({ type: 'updateStore', payload: { areaList: areaList } });
        }
      } catch (e) {

      }
    }
  },

  // 存入状态机
  reducers: {
    updateStore(preState, action) {
      return Object.assign({}, preState, action.payload)
    },
  }
}

export default tmpModule
